import java.math.BigInteger;
import java.util.ArrayList;


public class Ex6_5 {

	/**
	 * PLEASANTLY
	   -ME--AN-LY
	   https://class.coursera.org/bioinformatics-001/forum/thread?thread_id=768
	 */
	public Ex6_5() {
		resultV = "";
		resultW = "";
	}

	public String resultV, resultW;
	public 	Integer back[][];

	public static void main(String[] args) {
		Ex6_5 exer = new Ex6_5();
		//ArrayList<String> inputs = Utils.readLines("C:\\Users\\josemfer\\Downloads\\dataset_79_14.txt");
		ArrayList<String> inputs = Utils.readLines("C:\\Users\\josemfer\\Downloads\\input_6_5.txt");
		String v = inputs.get(0);
		String w = inputs.get(1);			
		Integer[][] scores = exer.getScores(v, w);
		Integer finalScore = scores[v.length()][w.length()];
		exer.getOutputLCS(exer.back,v,w,v.length(),w.length());
		Utils.writeFile( finalScore + "\r\n" 
				+ exer.resultV + "\r\n" 
				+ exer.resultW, 
				"C:\\Users\\josemfer\\Downloads\\out_6_5.txt");
		System.out.println(finalScore);
		System.out.println(exer.resultV);
		System.out.println(exer.resultW);

	}


	private void getOutputLCS(Integer[][] backtrack, 
			String v, String w, Integer i, Integer j) {
		//System.out.println("i=" + i + "   j=" + j);
		if (i == 0 && j == 0) {
			return;
		}
		if (backtrack[i][j] == 0 ) { // '0' = down
			getOutputLCS(backtrack,v,w,i-1,j);			
			resultV += v.charAt(i.intValue()-1);
			resultW += "-";

		} else if (backtrack[i][j] == 1) {   // '1' = right 
			getOutputLCS(backtrack,v,w,i,j.intValue()-1);
			resultV += "-";
			resultW += w.charAt(j-1);

		} else if (backtrack[i][j] == 2) {
			getOutputLCS(backtrack,v,w,i-1,j-1);   //  '2' = diagonal
			resultV += v.charAt(i.intValue()-1);
			resultW += w.charAt(j.intValue()-1);			
		}		
	}	

	/*
	 *   '0' = down   '1' = right   '2' = diagonal
	 */
	private Integer[][] getScores(String v, String w) {				
		Integer sigma = 5;
		String blosum62 = "ACDEFGHIKLMNPQRSTVWY";	
		Integer[][] blosumMatrix = 
			 Utils.readMatrix("C:\\Users\\josemfer\\Downloads\\BLOSUM62.txt");
		Integer n = v.length();
		Integer m = w.length();
		Integer s[][] = new Integer[n+1][m+1];
		this.back = new Integer[n+1][m+1];		
		
		for (int i=0;i<=n;i++) {
			s[i][0] = -sigma*i;
			this.back[i][0] = 0; // down
		}
		for (int j=0;j<=m;j++) {
			s[0][j] = -sigma*j;
			this.back[0][j] = 1; // right
		}
		for (int i=1;i<=n;i++) {
			System.out.println(i);		
			
			for (int j=1;j<=m;j++) {
				// calculate down and right
				Integer down =  s[i-1][j] - sigma;
				Integer right = s[i][j-1] - sigma;
				// calculate diag				
				char vi = v.charAt(i-1);
				char wj = w.charAt(j-1);
				Integer blosum_i = blosum62.indexOf(vi);
				Integer blosum_j = blosum62.indexOf(wj);
				Integer diag =  s[i-1][j-1] + blosumMatrix[blosum_i][blosum_j];
				//save score and backtrack matrix
				if ((down.compareTo(right) > 0) &&  (down.compareTo(diag)>0)) {
					s[i][j] = down;
					back[i][j] = 0;
				} else if ((right.compareTo(down) > 0) &&  (right.compareTo(diag)>0) ) {
					s[i][j] = right;
					back[i][j] = 1;
				} else {
					s[i][j] = diag;
					back[i][j] = 2;
				}				
			}				
		}
		return s;
	}

}
